import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入页面组件
// import Login from '@/views/Login'
// import Layout from '@/views/Layout'
// import Users from '@/views/User/Users'
// import Rights from '@/views/Auth/Rights'
// import Roles from '@/views/Auth/Roles'
// import Categories from '@/views/Goods/Categories'
// import Params from '@/views/Goods/Params'
// import Goods from '@/views/Goods/Goods'
// import GoodsAdd from '@/views/Goods/GoodsAdd'
// import Order from '@/views/Order'
// import Reports from '@/views/Reports'
// import Auth from '@/views/Auth'
// 重写push方法，解决同路由跳转报错的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login'),
    meta: { title: '登录' }
  },
  {
    path: '/',
    name: 'Layout',
    component: () => import('@/views/Layout'),
    redirect: '/users',
    meta: { title: '主页' },
    // 嵌套路由
    children: [
      {
        path: '/users',
        name: 'Users',
        component: () => import('@/views/User/Users'),
        meta: { title: '用户管理' }
      },
      {
        path: '/rights',
        redirect: '/auth/rights'
      },
      {
        path: '/roles',
        redirect: '/auth/roles'
      },
      {
        path: '/auth',
        name: 'auth',
        component: () => import('@/views/Auth/index'),
        meta: { title: '权限管理' },
        redirect: '/auth/roles',
        children: [
          {
            path: '/auth/rights',
            name: 'Rights',
            component: () => import('@/views/Auth/Rights'),
            meta: { title: '权限信息' }
          },
          {
            path: '/auth/roles',
            name: 'Roles',
            component: () => import('@/views/Auth/Roles'),
            meta: { title: '角色信息' }
          }
        ]
      },
      {
        path: '/categories',
        name: 'Categories',
        component: () => import('@/views/Goods/Categories'),
        meta: { title: '分类管理' }
      },
      {
        path: '/params',
        name: 'Params',
        component: () => import('@/views/Goods/Params'),
        meta: { title: '参数管理' }
      },
      {
        path: '/goods',
        name: 'Goods',
        component: () => import('@/views/Goods/Goods'),
        meta: { title: '商品管理' }
      },
      {
        path: '/goods/add',
        name: 'GoodsAdd',
        component: () => import('@/views/Goods/GoodsAdd'),
        meta: { title: '商品添加' }
      },
      {
        path: '/orders',
        name: 'Order',
        component: () => import('@/views/Order'),
        meta: { title: '订单管理' }
      },
      {
        path: '/reports',
        name: 'Reports',
        component: () => import('@/views/Reports'),
        meta: { title: '数据统计' }
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

export default router
